<?php
/**
 * 权限资源分类
 * @return array
 */
function getRoleMenu(){
    return  array('system'=>'系统设置','content'=>'内容管理','goods'=>'商品中心','member'=>'会员中心',
        'order'=>'订单中心','marketing'=>'营销推广','tools'=>'插件工具','count'=>'统计报表'
    );
}

function getMenuArr(){
    $menuArr = include APP_PATH.'agent/config/menu.php';
    $act_list = session('act_list');
    if($act_list != 'all' && !empty($act_list)){
        $right = M('system_menu')->where("id in ($act_list)")->cache(true)->getField('right',true);
        foreach ($right as $val){
            $role_right .= $val.',';
        }
        foreach($menuArr as $k=>$val){
            foreach ($val['child'] as $j=>$v){
                foreach ($v['child'] as $s=>$son){
                    if(strpos($role_right,$son['op'].'@'.$son['act']) === false){
                        unset($menuArr[$k]['child'][$j]['child'][$s]);//过滤菜单
                    }
                }
            }
        }
        foreach ($menuArr as $mk=>$mr){
            foreach ($mr['child'] as $nk=>$nrr){
                if(empty($nrr['child'])){
                    unset($menuArr[$mk]['child'][$nk]);
                }
            }
        }
    }
    return $menuArr;
}



/**
 * 获取缓存或者更新缓存
 * @param string $config_key 缓存文件名称
 * @param array $data 缓存数据  array('k1'=>'v1','k2'=>'v3')
 * @return array or string or bool
 */
function tpCache($config_key,$data = array()){
    $param = explode('.', $config_key);
    if(empty($data)){
        //缓存文件不存在就读取数据库
        $res = \think\Db::name('configs')->where("inc_type",$param[0])->select();
        if($res){
            foreach($res as $k=>$val){
                $config[$val['name']] = $val['value'];
            }
        }
        return $config;
    }else{
        //更新缓存
        $result = \think\Db::name('configs')->where('inc_type',$param[0])->select();
        if($result){
            foreach($result as $val){
                $temp[$val['name']] = $val['value'];
            }

            foreach($data as $k=>$v){
                $newArr = array('name'=>$k,'value'=>trim($v),'inc_type'=>$param[0]);
                if(!isset($temp[$k])){
                    \think\Db::name('configs')->insert($newArr);//新key数据插入数据库
                }else{
                    if($v!=$temp[$k]){
                        \think\Db::name('configs')->where('name',$k)->update($newArr);//缓存key存在且值有变更新此项
                    }
                }
            }

            //更新后的数据库记录
            $newRes = \think\Db::name('configs')->where("inc_type", $param[0])->select();
            foreach ($newRes as $rs){
                $newData[$rs['name']] = $rs['value'];
            }
        }else{
            foreach($data as $k=>$v){
                $newArr[] = array('name'=>$k,'value'=>trim($v),'inc_type'=>$param[0]);
            }
            \think\Db::name('configs')->insertAll($newArr);//插入configs数据

            $newData = $data;
        }
//        return $newData;
    }
    /**
     * 获取商家审核状态
     * @param $value
     * @return mixed
     */
    function getShopAudit(){
        $arr = [1=>'通过审核',0=>'待审核',-1=>'未通过审核'];
        return 'sdfd';
    }
}

